package com.example.tripmasternew

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class SplashActivity : AppCompatActivity() {

    private lateinit var countdownCircle: TextView
    private val countdownTime = 3
    private var currentCount = countdownTime
    private val handler = Handler(Looper.getMainLooper())

    private val countdownRunnable = object : Runnable {
        override fun run() {
            if (currentCount > 0) {
                countdownCircle.text = currentCount.toString()
                currentCount--
                handler.postDelayed(this, 1000)
            } else {
                goToMainActivity()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.splash_activity)
        val instructionLabel = findViewById<TextView>(R.id.instructionLabel)
        instructionLabel.setOnClickListener {
            handler.removeCallbacks(countdownRunnable) // ⛔️ Ferma il countdown
            val intent = Intent(this, HelpActivity::class.java)
            startActivity(intent)
        }

        countdownCircle = findViewById(R.id.countdownCircle)
        val countdownCircle = findViewById<TextView>(R.id.countdownCircle)
        countdownCircle.setOnClickListener {
            handler.removeCallbacks(countdownRunnable)
            val intent = Intent(this, HelpActivity::class.java)
            startActivity(intent)
            finish()
        }
        countdownCircle.text = countdownTime.toString()

        handler.postDelayed(countdownRunnable, 1000)
    }

    private fun goToMainActivity() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }
}
